package com.woniumall.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.woniumall.pojo.*;
import com.woniumall.service.GoodsService;
import com.woniumall.service.impl.GoodsServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @auther linke
 * @create 2021-10-28 16:47
 * 书籍处理类
 */
@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet{
       //获取GoodsService服务对象
       private  GoodsService goodsService = new GoodsServiceImpl();
    /**
     * 首页查询新书程序
     * @param request
     * @param response
     * @return
     */
    //首页新书展示
    public void findNewGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Goods> list = goodsService.findNewGoods();
        //调用父类中的工具类响应回json
        responseUtil(response,list);
    }
    //书籍详情评论展示
    public void comment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String goodsid = request.getParameter("goodsid");//获取书本id
        /*PageInfo<Remark> list = goodsService.showComment(Integer.parseInt(goodsid),1);
        request.setAttribute("commentList",list);*/
/*        Massage msg = new Massage();
        if (list == null){
            msg.setFlag(false);
            responseUtil(response,msg);
        }else {
            //调用父类中的工具类响应回json
            responseUtil(response,list);
        }*/
    }
    //书籍评论添加
    public void addcomment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String goodsid = request.getParameter("goodsid");//获取书本id
        String comment = request.getParameter("comment");//获取评论内容
        User user = (User)request.getSession().getAttribute("user");//获取userid
        Massage msg = new Massage();
        if(user == null ){
            msg.setFlag(false);
            msg.setMassage("添加失败");
            responseUtil(response,msg);
            return;
        }
        int row = goodsService.addComment(Integer.parseInt(goodsid),comment,user.getId());

        if(row < 1){
            msg.setFlag(false);
            msg.setMassage("添加失败");
            responseUtil(response,msg);
        }else {
            msg.setFlag(true);
            msg.setMassage("添加成功");
            responseUtil(response,msg);
        }
    }
    //首页热门图书展示
    public void findHotGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //调用方法
        List<Goods> list = goodsService.findHotGoods();
        //调用父类中的工具类响应回json
        responseUtil(response,list);
    }

    //商品详情
    public void details(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        Goods goods =  goodsService.findById(Integer.parseInt(id));
        PageInfo<Remark> list = goodsService.showComment(Integer.parseInt(id),1);
        request.setAttribute("commentList",list);//评论显示
        //comment(request,response);
        //增加热门排行榜数据
        List<Goods> ranklist = goodsService.findBysalenums(2);
        request.setAttribute("ranklist",ranklist);

        request.setAttribute("goods",goods);
        request.getRequestDispatcher("/jspPage/detailPage.jsp").forward(request,response);
        System.out.println(goods);
    }
    //商品分类
    public void findPageByCid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String cid = request.getParameter("cid");
        String currentPage = request.getParameter("currentPage");
        PageBean<Goods> pageBean = goodsService.findPageByCid(Integer.parseInt(cid),Integer.parseInt(currentPage));
        // System.out.println(pageBean);

        //增加热门排行榜数据
        List<Goods> ranklist = goodsService.findBysalenums(8);
        request.setAttribute("ranklist",ranklist);

        request.setAttribute("pageBean",pageBean);
        request.getRequestDispatcher("/jspPage/CategoryPage.jsp").forward(request,response);
    }

    //查询更多新书并分页显示
    public void findNewGoodsPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String currentPage = request.getParameter("currentPage");
        //转成插件自动分页数组
        PageInfo<Goods> plist =   goodsService.findNewGoodsPage(Integer.parseInt(currentPage));
        System.out.println(plist);

        //增加热门排行榜数据
        List<Goods> ranklist = goodsService.findBysalenums(8);
        request.setAttribute("ranklist",ranklist);

          //存入作用域
          request.setAttribute("plist",plist);
          request.getRequestDispatcher("/jspPage/CategoryPageNew.jsp").forward(request,response);
    }
    //查询更多热门图书并分页显示
    public void findHotGoodsPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String currentPage = request.getParameter("currentPage");
        //转成插件自动分页数组
        PageInfo<Goods> plist =   goodsService.findHotGoodsPage(Integer.parseInt(currentPage));
        System.out.println(plist);

        //增加热门排行榜数据
        List<Goods> ranklist = goodsService.findBysalenums(8);
        request.setAttribute("ranklist",ranklist);

        //存入作用域
        request.setAttribute("plist",plist);
        request.getRequestDispatcher("/jspPage/CategoryPageHot.jsp").forward(request,response);
    }
    //模糊搜索查询
    public void search(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取搜索关键字
        String keyword = request.getParameter("keyword");
        //获取分页关键字
        String currentPage = request.getParameter("currentPage");

        //转成插件自动分页数组
        PageInfo<Goods> plist =  goodsService.search(keyword,Integer.parseInt(currentPage));
        System.out.println(plist);

        //增加热门排行榜数据
        List<Goods> ranklist = goodsService.findBysalenums(8);
        request.setAttribute("ranklist",ranklist);

        //存入作用域
        request.setAttribute("plist",plist);
        request.getRequestDispatcher("/jspPage/CategoryPageSearch.jsp").forward(request,response);
    }

}
